package com.health.db.dao.user.impl;

import java.util.ArrayList;
import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IVoiceOnDemondListDao;
import com.health.db.mapper.VoiceOnDemondListMapper;
import com.health.db.model.VoiceOnDemondList;
import com.health.db.model.VoiceOnDemondListExample;
import com.health.redis.RedisConstants;

/**
 * 孙奶奶音频
 * 缓存适用
 * @author YC
 *
 */
public class VoiceOnDemondListDaoImpl extends ServiceInterface implements
		IVoiceOnDemondListDao {

	VoiceOnDemondListMapper voiceOnDemondListMapper;

	public VoiceOnDemondListMapper getVoiceOnDemondListMapper() {
		return voiceOnDemondListMapper;
	}

	public void setVoiceOnDemondListMapper(
			VoiceOnDemondListMapper voiceOnDemondListMapper) {
		this.voiceOnDemondListMapper = voiceOnDemondListMapper;
	}

	/**
	 * 
	 * example object
	 * {
	 * voiceOnDemondId   上级ID
	 * pageSize    查询量   
	 * offset      偏移位置
	 * isUsedFlag  是否可用
	 * isIssueFlag  是否发布
	 * }
	 */
	@Override
	public List<VoiceOnDemondList> selectByExample(
			VoiceOnDemondListExample example) {
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String key = RedisConstants.VoiceOnDemondListMapper  
					+":voiceOnDemondId:"+example.getVoiceOnDemondId()
					+":PS:"+example.getPageSize()
					+":OS:"+example.getOffset();
		Object object = null;
		List<VoiceOnDemondList> list = new ArrayList<VoiceOnDemondList>();
		if (redisOperatiorLogic.isJedisIsConnected()) {
			// get data from redis
			object = redisOperatiorLogic.readObject(key,getPublicRedisGroupNo("1"));
		}

		if (null != object ) {
			if (!object.equals("")) {
				list = (List<VoiceOnDemondList>) object;
			}
		} else {
		 
			list = voiceOnDemondListMapper.selectByExample(example);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list,	getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

	/**
	 * recId 主键
	 */
	@Override
	public VoiceOnDemondList selectByPrimaryKey(Long recid) {
		
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String key = RedisConstants.VoiceOnDemondListMapper +":voiceOnDemondId:"+recid;
		Object object = null;
		VoiceOnDemondList list = new VoiceOnDemondList();
		if (redisOperatiorLogic.isJedisIsConnected()) {
			object = redisOperatiorLogic.readObject(key,getPublicRedisGroupNo("1"));
		}
		if (null != object) {
			if (!object.equals("")) {
				list = (VoiceOnDemondList) object;	
			}
		} else {
		 
			list =voiceOnDemondListMapper.selectByPrimaryKey(recid);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list,	getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

	/**
	 * recId 主键
	 * 更新收听量  每次调用 增加一次收听量
	 */
	@Override
	public int updateListenAmount(Long recid) {
		// TODO Auto-generated method stub
		return voiceOnDemondListMapper.updateListenAmount(recid);
	}

}
